<template>
	<div class="edit-popup">
		<popup ref="popupRef" :title="popupTitle" :async="true" width="650px" @confirm="handleSubmit"
			@close="handleClose">
			<el-form ref="formRef" :model="formData" label-width="100px" :rules="formRules">
				<el-form-item label="会议室名称" prop="name">
					<el-input v-model="formData.name" placeholder="请输入会议室名称" clearable />
				</el-form-item>
				<el-form-item label="容纳人数" prop="person_num">
					<el-input-number v-model="formData.person_num" placeholder="请输入容纳人数" clearable min="0" step="1" style="width: 50%;" />
				</el-form-item>
				<el-form-item label="设备" prop="device_tags">
					<el-checkbox-group v-model="formData.device_tags">
						<el-checkbox label="1">电视</el-checkbox>
						<el-checkbox label="2">投影</el-checkbox>
						<el-checkbox label="3">白板</el-checkbox>
					</el-checkbox-group>
				</el-form-item>
				<el-form-item label="可使用范围" prop="can_user_tags" required>
					<el-checkbox-group v-model="formData.can_user_tags">
						<el-checkbox label="1">助贷之家，承租人员</el-checkbox>
						<el-checkbox label="4">当前场地渠道人员</el-checkbox>
					</el-checkbox-group>
				</el-form-item>
				<el-form-item label="启用状态" prop="is_enable" required>
					<el-radio-group v-model="formData.is_enable">
						<el-radio :label="1">启用</el-radio>
						<el-radio :label="0">停用</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
		</popup>
	</div>
</template>
<script lang="ts" setup>
	import type { FormInstance } from 'element-plus'
	import { officeEdit, officeAdd } from '@/api/sharedoffice/sharedoffice';
	import Popup from '@/components/popup/index.vue';
	import { getModulesKey } from '@/router'

	const emit = defineEmits(['success', 'close'])
	const formRef = shallowRef<FormInstance>()
	const popupRef = shallowRef<InstanceType<typeof Popup>>()
	const mode = ref('add')
	const popupTitle = computed(() => {
		return mode.value == 'edit' ? '编辑会议室' : '新增会议室'
	})

	const componentsOptions = ref(getModulesKey())
	const querySearch = (queryString : string, cb : any) => {
		const results = queryString
			? componentsOptions.value.filter((item) =>
				item.toLowerCase().includes(queryString.toLowerCase())
			)
			: componentsOptions.value
		cb(results.map((item) => ({ value: item })))
	}
	const formData = reactive({
		id: '',
		//场地id
		office_place_id: null,
		//名称
		name: '',
		//容纳人数
		person_num: null,
		//设备
		device_tags: [],
		// 可使用范围
		can_user_tags: [],
		//启用状态
		is_enable: 1
	})

	const formRules = {
		name: [
			{
				required: true,
				message: '请输入会议室名称',
				trigger: 'blur'
			}
		],
		person_num: [
			{
				required: true,
				message: '请输入容纳人数',
				trigger: 'blur'
			}
		],
		device_tags: [
			{
				required: true,
				message: '请选择设备',
				trigger: 'blur'
			}
		],
		can_user_tags: [
			{
				required: true,
				message: '请选择可使用范围',
				trigger: 'blur'
			}
		]
	}
	const menuOptions = ref<any[]>([])

	const handleSubmit = async () => {
		await formRef.value?.validate()
		let newFormData = JSON.parse(JSON.stringify(formData));
		mode.value == 'edit' ? await officeEdit(newFormData) : await officeAdd(newFormData)
		popupRef.value?.close()
		emit('success')
	}

	const open = (type = 'add') => {
		mode.value = type
		popupRef.value?.open()
	}
	
	const setFormData = (data : Record<any, any>) => {
		for (const key in formData) {
			if (data[key] != null && data[key] != undefined) {
				//@ts-ignore
				formData[key] = data[key]
			}
		}
	}

	const getDetail = async (row : Record<string, any>) => {
		setFormData(row)
	}

	const handleClose = () => {
		emit('close')
	}

	defineExpose({
		open,
		setFormData,
		getDetail
	})
</script>

<style scoped lang="scss"></style>